package com.itheima.Utils;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * 阿里云 上传图片
 * @author GY
 */
@Slf4j
@Component
public class AliyunOssUtils {

    @Value("${aliyun.oss.domain}")
    String domain;
    @Value("${aliyun.oss.endpoint}")
    String endpoint;
    @Value("${aliyun.oss.bucketName}")
    String bucketName;
    @Value("${aliyun.oss.region}")
    String region;
    @Value("${aliyun.oss.accessKeyId}")
    String accessKeyId;
    @Value("${aliyun.oss.secretAccessKey}")
    String secretAccessKey;
    // String domain = "oss-cn-hangzhou.aliyuncs.com";
    //
    // // 设置 OSS Endpoint 和 Bucket 名称
    // String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    // String bucketName = "test-hz-gy";
    // // 替换为您的 Bucket 区域
    // String region = "cn-hangzhou";
    // // 创建 OSSClient 实例
    // String accessKeyId = "LTAI5t6UTXDt5rzt7n3yQXKS";
    // String secretAccessKey = "LBtTgSwEcp0Eer9xb6X57Wn1GNghSC";

    /**
     * 返回文件上传到服务器的路径
     * @param file
     * @return
     */
    public String uploadFile(MultipartFile file) {
        // 1.参数校验
        if(ObjectUtils.isEmpty(file) || file.isEmpty()){
            throw new RuntimeException("上传文件不能为空");
        }
        String filename = file.getOriginalFilename();
        if(ObjectUtils.isEmpty(filename)){
            throw new RuntimeException("上传文件名错误！");
        }
        String path = null;
        OSS ossClient = null;
        try {
            CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, secretAccessKey);
            ossClient = OSSClientBuilder.create()
                    .endpoint(endpoint)
                    .credentialsProvider(credentialsProvider)
                    .credentialsProvider(credentialsProvider)
                    .region(region)
                    .build();
            //.jpg
            String suffix = filename.substring(filename.lastIndexOf("."));
            //1.2.j3.fopsdf.jpg    --->   0256e2648807434fa54dd66597c69e37.jpg
            filename = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
            log.info("上传到服务器的文件名:{}", filename);
            PutObjectResult result = ossClient.putObject(bucketName, filename, file.getInputStream());
            log.info("result:{}", result);
            path = "https://" + bucketName + "." + domain + "/" + filename;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("{}", e.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return path;
    }


    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            UUID uuid = UUID.randomUUID();
            System.out.println(uuid);
        }

    }
}